Skip to content

Conversation

dianqk
Copy link
Member

@dianqk dianqk commented Oct 19, 2025

Fixes #141313 and #141313 (comment).

I agree with what #147844 (comment) mentioned:

We can reason with the value behind a reference because it is UB to directly assign to the underlying local while the reference is live. We allow creating new derefs, this means extending the liveness of references, so we are creating UB.

I don't think we should ban introducing new derefs, since this may prevent many optimizations.

r? cjgillot

@rustbot
Copy link
Collaborator

rustbot commented Oct 19, 2025

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 19, 2025
/// Check that we allow dereferences in the RHS if the LHS is a stable projection.
// This introduces copy overlapping if dereferencing `_2` or `_4`.
#[custom_mir(dialect = "runtime")]
fn stable_projection(_1: (Adt,)) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cjgillot I just found this still introduces copy overlapping.

@dianqk
Copy link
Member Author

dianqk commented Oct 19, 2025

@bors try @rust-timer queue

@rust-timer

This comment has been minimized.

rust-bors bot added a commit that referenced this pull request Oct 19, 2025
GVN: Only introduce new derefs if the immutable borrow is always valid.
@rust-bors

This comment has been minimized.

@rustbot rustbot added the S-waiting-on-perf Status: Waiting on a perf run to be completed. label Oct 19, 2025
@cjgillot
Copy link
Contributor

I'd have kept introducing Value::Projection(Deref) as exists, but forbidden introducing Deref projections in try_as_place. What do you think?

@rust-bors
Copy link

rust-bors bot commented Oct 19, 2025

☀️ Try build successful (CI)
Build commit: efb5f98 (efb5f98020c7392070aae01c719ed60b78144ff5, parent: c6efb9019b3169fc672248339dbbf13e6a134de3)

@rust-timer

This comment has been minimized.

@rust-timer
Copy link
Collaborator

Finished benchmarking commit (efb5f98): comparison URL.

Overall result: ❌✅ regressions and improvements - please read the text below

Benchmarking this pull request means it may be perf-sensitive – we'll automatically label it not fit for rolling up. You can override this, but we strongly advise not to, due to possible changes in compiler perf.

Next Steps: If you can justify the regressions found in this try perf run, please do so in sufficient writing along with @rustbot label: +perf-regression-triaged. If not, please fix the regressions and do another perf run. If its results are neutral or positive, the label will be automatically removed.

@bors rollup=never
@rustbot label: -S-waiting-on-perf +perf-regression

Instruction count

Our most reliable metric. Used to determine the overall result above. However, even this metric can be noisy.

mean range count
Regressions ❌
(primary)
0.8% [0.8%, 0.8%] 1
Regressions ❌
(secondary)
- - 0
Improvements ✅
(primary)
-0.3% [-0.4%, -0.3%] 3
Improvements ✅
(secondary)
-0.3% [-0.3%, -0.3%] 1
All ❌✅ (primary) -0.1% [-0.4%, 0.8%] 4

Max RSS (memory usage)

Results (primary 4.1%, secondary 0.8%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
4.1% [2.8%, 5.3%] 2
Regressions ❌
(secondary)
0.8% [0.8%, 0.8%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) 4.1% [2.8%, 5.3%] 2

Cycles

Results (secondary 3.2%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.2% [3.2%, 3.2%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

Results (primary 0.0%, secondary -0.0%)

A less reliable metric. May be of interest, but not used to determine the overall result above.

mean range count
Regressions ❌
(primary)
0.2% [0.0%, 1.0%] 11
Regressions ❌
(secondary)
0.0% [0.0%, 0.2%] 15
Improvements ✅
(primary)
-0.1% [-0.5%, -0.0%] 17
Improvements ✅
(secondary)
-0.1% [-0.1%, -0.0%] 9
All ❌✅ (primary) 0.0% [-0.5%, 1.0%] 28

Bootstrap: 472.659s -> 474.116s (0.31%)
Artifact size: 390.57 MiB -> 391.13 MiB (0.14%)

@rustbot rustbot added perf-regression Performance regression. and removed S-waiting-on-perf Status: Waiting on a perf run to be completed. labels Oct 19, 2025
@dianqk
Copy link
Member Author

dianqk commented Oct 19, 2025

I'd have kept introducing Value::Projection(Deref) as exists, but forbidden introducing Deref projections in try_as_place. What do you think?

Oh, I think this is a great insight. IIUC, I can remove invalid_derefs and introduce a borrow-reaching definition that is used in try_as_place. This should allow us to preserve derefs in the RPO A-B(terminator is call)-C.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

perf-regression Performance regression. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GVN misunderstands aliasing, can create overlapping assignments (again)

4 participants